package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberAuditBean;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.ui.fragment.MemberInviteOrApplyRecordFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class MemberInviteOrApplyRecordModel extends BaseViewModel {

    MemberInviteOrApplyRecordFragment fragment;
    MemberRepertory repertory;
    int current;

    public MemberInviteOrApplyRecordModel(MemberInviteOrApplyRecordFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    /**
     * 获取企业列表
     *
     * @param curPageType 0 邀请会员；1申请会员
     * @param isLoadMore  是否加载更多
     */
    public void getInviteOrApplyRecords(int curPageType, boolean isLoadMore, String text, String businessNo,
                                        String areaNo, String inviteOrApplyStatusNo, String relationNo) {
        showOrHideDialog(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("name", StringUtil.isEmpty(text) ? "" : text); // 企业名称
        map.put("area", areaNo);
        map.put("relation", relationNo);
        map.put("limit", 20);
        map.put("page", currentPage);
        map.put("status", inviteOrApplyStatusNo);
        if (curPageType == 0) {
            map.put("joinWay", "2"); // 途径方式 1:申请 2：邀请
        } else {
            map.put("joinWay", "1"); // 途径方式 1:申请 2：邀请
        }
        map.put("serviceType", businessNo);
        map.put("provinceNo", areaNo);
        repertory.getApi().getMixRecordMemberByPage(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberAuditBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberAuditBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberAuditBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmpty();
            }
        });
        /*call.enqueue(new ICallBack<BaseEntity<ListMResponse<List<QualityMemberBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<QualityMemberBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<QualityMemberBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmpty();
            }
        });*/
    }

    // 获取个人企业申请记录
    public void getPersonApplyCompanyRecordList(int curPageType, boolean isLoadMore, String text, String businessNo,
                                                String areaNo, String inviteOrApplyStatusNo, String relationNo) {
        showOrHideDialog(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("name", StringUtil.isEmpty(text) ? "" : text); // 企业名称
        map.put("area", areaNo);
        map.put("relation", relationNo);
        map.put("limit", 20);
        map.put("page", currentPage);
        map.put("status", inviteOrApplyStatusNo);
        if (curPageType == 0) {
            map.put("joinWay", "2"); // 途径方式 1:申请 2：邀请
        } else {
            map.put("joinWay", "1"); // 途径方式 1:申请 2：邀请
        }
        map.put("serviceType", businessNo);
        map.put("provinceNo", areaNo);
        repertory.getApi().getPersonApplyCompanyRecordList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberPersonBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberPersonBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecyclerPerson(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberPersonBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmpty();
            }
        });
    }

    /**
     * 1.no,为列表数据项no
     * 2.companyNo,为列表数据项companyNo
     * 3.joinWay      1申请   2邀请
     */
    public void getUrlSetting(String param, String endUrl) {
        showOrHideDialog(true);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + endUrl;
                MemberConfig.getInstance().startWebViewX5(fragment.getContext(), url, 101);
            }
        });
    }

    // joinWay      1申请   2邀请

    /**
     * ### 申请记录--个人申请企业
     * 路由地址:``/personalRecord``
     * 需要传参:
     * 1.no,为列表数据项no
     * 2.companyNo,为列表数据项companyNo
     * 3.joinWay      1申请   2邀请
     *
     * @param param
     * @param personEndUrl
     */
    public void getUrlPersonSetting(String param, String personEndUrl) {
        showOrHideDialog(true);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + personEndUrl;
                MemberConfig.getInstance().startWebViewX5(fragment.getContext(), url, 101);
            }
        });
    }

    /**
     * 获取审批流程状态
     *
     * @param approveType 邀请加盟审批类型 inviteJoinCompany
     *                    申請加盟审批类型 applyJoinCompany
     *                    会员企业调级 memberEvaluate
     *                    会员解除申请 memberRelieve
     */
    public void getProcessStatus(String companyNo, String approveType) {
        showOrHideDialog(true);
        repertory.getApi().getProcessStatus(companyNo, approveType).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                if (!TextUtils.isEmpty(status)) {
                    fragment.updateProcessStatus(status);
                } else {
                    MToast.showToast(fragment.getContext(), "网络异常，请稍后重试");
                }
            }
        });
    }


    public void getBusinessTypes(int type) {
        showOrHideDialog(true);
        repertory.getApi().getServiceTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type) {
        showOrHideDialog(true);
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, new CommonPopEntity("", "全国"));
                    fragment.setChooseItemData(type, entities);
                }
            }
        });
    }

    public void getInviteOrApplyStatus(int curPage, int type) {
        // 邀请状态：待审核、审核通过、审核未通过
        // 申请状态：待审核、审核通过、审核未通过
        showOrHideDialog(true);
        repertory.getApi().getCheckStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }

    public void getMemberRelation(int type) {
        // 供需关系 1.供方会员 2.需方会员
        List<CommonPopEntity> data = new ArrayList<>();
        data.add(new CommonPopEntity("", "全部"));
        data.add(new CommonPopEntity("1", "供方会员"));
        data.add(new CommonPopEntity("2", "需方会员"));
        fragment.setChooseItemData(type, data);
    }

    //获取经营类型类目
    public void getCompanyServiceTypeCateGory(int type){
        repertory.getApi().getCompanyServiceTypeCateGory().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }
}
